package net.betou.trade.main.dao.impl;

import java.util.List;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.SuggAnswer;
import net.betou.trade.main.dao.SuggAnswerDao;

import org.springframework.stereotype.Repository;

@Repository
public class SuggAnswerDaoImpl extends HibernateBaseDao< SuggAnswer, Integer > implements SuggAnswerDao{

	@Override
	protected Class< SuggAnswer > getEntityClass( ) {
		return SuggAnswer.class;
	}

	public SuggAnswer deleteById( Integer id ) {
		SuggAnswer bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}
		
		return bean;
	}

	public SuggAnswer findById( Integer id ) {
		return get(id);
	}

	public SuggAnswer save( SuggAnswer bean ) {
		getSession( ).save( bean );
		return bean;
	}
	
	public Pagination getPage( Integer selfId, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ){
		
		Finder f = Finder.create( " from SuggAnswer bean where 1=1 " );
		if( selfId != null ){
			f.append( " and bean.admin.id=:id" );
			f.setParam( "id", selfId );
		}
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find( f, pageNo, pageSize );
	}
	
	@SuppressWarnings("unchecked")
	public List<SuggAnswer> findOnSugg( Integer id ){
		String hql = "from SuggAnswer bean where bean.sugg.id=? ";
		return this.find(hql, id);
	}
}